/**
 * Copyright (c) 2009-2012 XTEAM
 * All rights reserved.
 */
/**
 * File：GoodsAction.java
 * History:
 *         2012-12-19: Initially created, Chrise.
 */
package com.xteam.ecsystem.action;

import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.xteam.ecsystem.base.Constant;
import com.xteam.ecsystem.base.UserRole;
import com.xteam.ecsystem.entities.BaseMemberBase;
import com.xteam.ecsystem.service.front.FrontCartService;
import com.xteam.ecsystem.utils.AppConfig;
import com.xteam.ecsystem.utils.CookiesUtils;
import com.xteam.ecsystem.utils.FrontUtil;
import com.xteam.utils.UUID;

/**
 * 购物车请求控制层。
 * @author ls
 */
@Controller
public class CartAction {
	
	@Autowired
	private FrontCartService fcService;
	
	/**
	 * 
	 * 加入购物车
	 * @author ls
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/addToCart.xhtml", method = RequestMethod.POST)
	public void addToCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//用户ID
		String userId = "";
		//用户角色
		int userRole = UserRole.Visitor.value();
		BaseMemberBase bmb = (BaseMemberBase) request.getSession().getAttribute(Constant.VAR_MEMBER.value());
		if(bmb != null){
			userId = bmb.getBmbId();
			userRole = UserRole.Member.value();
		}else{
			Cookie cookie = CookiesUtils.getCookieByName(request, "XMECS_USERID");
			if(cookie!=null){
				userId = java.net.URLDecoder.decode(cookie.getValue(), "UTF-8");
				cookie.setMaxAge(AppConfig.getCartMaxAge()*24*60*60);
			}else{
				userId = UUID.getUUID();
				cookie = new Cookie("XMECS_USERID",userId);
				cookie.setMaxAge(AppConfig.getCartMaxAge()*24*60*60);
				response.addCookie(cookie);
			}
		}
		//商品ID
		String id = FrontUtil.getStringParam(request, "id", "");
		//商品属性ID
		String attr = FrontUtil.getStringParam(request, "attr", "");
		//商品数量
		int num = FrontUtil.getIntegerParam(request, "num", 1);
		fcService.addToCart(id, attr, num, userId, userRole);
		
	}
	
	/**
	 * 
	 * 获取总价
	 * @author ls
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/getTotalPrice.xhtml", method = RequestMethod.POST)
	public void getTotalPrice(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//商品ID
		String id = FrontUtil.getStringParam(request, "id", "");
		//商品属性ID
		String attr = FrontUtil.getStringParam(request, "attr", "");
		//商品数量
		int num = FrontUtil.getIntegerParam(request, "num", 1);
		String totalPrice = fcService.getTotalPrice(id, attr, num);
		
		response.getWriter().write(totalPrice);
	}
	
	/**
	 * 
	 * 移除购物车
	 * @author ls
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/dropFromCart.xhtml", method = RequestMethod.GET)
	public void dropFromCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//商品ID
		String bciId = FrontUtil.getStringParam(request, "bciId", "");
		//用户ID
		String userId = "";
		BaseMemberBase bmb = (BaseMemberBase) request.getSession().getAttribute(Constant.VAR_MEMBER.value());
		if(bmb != null){
			userId = bmb.getBmbId();
		}else{
			Cookie cookie = CookiesUtils.getCookieByName(request, "XMECS_USERID");
			if(cookie!=null){
				userId = java.net.URLDecoder.decode(cookie.getValue(), "UTF-8");
			}
		}
		fcService.dropFromCart(bciId, userId);
		
	}
	
	/**
	 * 
	 * 清空购物车
	 * @author ls
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/clearCartByUserId.xhtml", method = RequestMethod.GET)
	public void clearCartByUserId(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//用户ID
		String userId = "";
		BaseMemberBase bmb = (BaseMemberBase) request.getSession().getAttribute(Constant.VAR_MEMBER.value());
		if(bmb != null){
			userId = bmb.getBmbId();
		}else{
			Cookie cookie = CookiesUtils.getCookieByName(request, "XMECS_USERID");
			if(cookie!=null){
				userId = java.net.URLDecoder.decode(cookie.getValue(), "UTF-8");
			}
		}
		fcService.clearCartByUserId(userId);
		
	}
	
	/**
	 * 
	 * 更新购物车
	 * @author ls
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/updateCartGoods.xhtml", method = RequestMethod.GET)
	public void updateCartGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//商品ID
		String bciId = FrontUtil.getStringParam(request, "bciId", "");
		//商品数量 
		int num = FrontUtil.getIntegerParam(request, "num", 0);
		//用户ID
		String userId = "0";
		BaseMemberBase bmb = (BaseMemberBase) request.getSession().getAttribute(Constant.VAR_MEMBER.value());
		if(bmb != null){
			userId = bmb.getBmbId();
		}else{
			Cookie cookie = CookiesUtils.getCookieByName(request, "XMECS_USERID");
			if(cookie!=null){
				userId = java.net.URLDecoder.decode(cookie.getValue(), "UTF-8");
			}
		}
		String totalPrice = fcService.updateCartGoods(bciId, num, userId);

		response.getWriter().write(totalPrice);
	}

}
